package com.qianxx.viewlibrary.switcher.slide;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.qianxx.viewlibrary.R;

/**
 * 功能描述：可滑动的开关控件
 */
public class SlideView extends LinearLayout implements Animation.AnimationListener, View.OnTouchListener {
    FrameLayout layout;
    ImageView btn, green, gray;
    Handler handler = new Handler();
    //控件实时宽度
    int mWidth;
    //当前水平坐标距离
    float downX;
    //动画移动距离
    int animOffset;
    //开关按钮是否开启状态
    boolean isOpen;
    //时延
    final int delayTime = 150;
    //监听器
    OnSlideViewListener listener;

    public SlideView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_slideview, this, false);
        addView(view);
        layout = (FrameLayout) view.findViewById(R.id.layout);
        layout.setOnTouchListener(this);
        btn = (ImageView) view.findViewById(R.id.btn);
        green = (ImageView) view.findViewById(R.id.green);
        gray = (ImageView) view.findViewById(R.id.gray);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
    }

    /**
     * 开启动画
     */
    private void starAnim(float form, float to) {
        TranslateAnimation anim = new TranslateAnimation(form, to, 0f, 0f);
        anim.setDuration(delayTime);
        anim.setFillEnabled(true);
        anim.setInterpolator(new LinearInterpolator());
        anim.setAnimationListener(this);
        btn.startAnimation(anim);
    }

    /**
     * 手柄移动
     *
     * @param position
     */
    protected void move(int position) {
        int marginLeft = position <= 0 ? 0 : position >= mWidth ? (mWidth - btn.getWidth()) : position;

        //手柄
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) btn.getLayoutParams();
        params.setMargins(marginLeft, 0, 0, 0);
        btn.requestLayout();

        //灰色背景
        FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) gray.getLayoutParams();
        param.setMargins(marginLeft, 0, 0, 0);
        gray.requestLayout();
    }

    /**
     * 开关是否开启
     *
     * @return
     */
    public boolean isOpen() {
        return isOpen;
    }

    /**
     * 设置监听器
     *
     * @param listener
     */
    public void setSlideViewListener(OnSlideViewListener listener) {
        this.listener = listener;
    }

    /**
     * 手柄弹性动画
     *
     * @param position
     */
    protected void up(int position) {
        animOffset = position < mWidth / 2 ? 0 : (mWidth - btn.getWidth());
        starAnim(0, animOffset);
    }

    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        move(animOffset);

        isOpen = animOffset <= 0 ? false : true;
        if (listener != null)
            listener.onState(isOpen);
    }

    /**
     * 设置开关状态
     *
     * @param isChecked
     */
    public void setChecked(final boolean isChecked) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                move(isChecked ? getWidth() : 0);
                isOpen = !isOpen;
            }
        }, delayTime * 3);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
                downX = motionEvent.getX();
                up((int) downX);
                break;
            case MotionEvent.ACTION_MOVE:
                downX = motionEvent.getX();
                move((int) downX);
                break;
            case MotionEvent.ACTION_CANCEL:
                up((int) downX);
                break;
        }
        return true;
    }

}
